require(['./config'], () =>{
    require(['template','header','footer'],(template)=>{

        class ShopCar{
            constructor (){
                
                this.init()
            }
            init(){
                let cartList = localStorage.getItem('cart')
                if (cartList){
                    this.cartList = JSON.parse(cartList)
                    $('#will_buy_shop').html(template('shopTemplate',{cartList:this.cartList}))
                    this.calcTotalMoney()
                    this.checksChagne()

                }else{
                    $('#nothing_shop').show()
                }
            }
            calcTotalMoney () {
                $('#Paymoney').html(this.cartList.reduce((money, cart) => {
                  if (cart.check) money += cart.price * cart.num
                  return money
                }, 0).toFixed(2))
              }
              checksChagne(){
                $('#all_check').prop('checked',this.cartList.every(cart =>{
                    return cart.check
                }))
                this.calcTotalMoney ()
                const that = this
                $('#will_buy_shop .shop_cont input:checkbox').on('change',function(){
                    
                    let id = $(this).parents('tr').attr('data_id')
                   
                        that.cartList = that.cartList.map(cart =>{
                            if(cart.id == id)cart.check = $(this).prop('checked')
                            return cart
                        })
                    localStorage.setItem('cart',JSON.stringify(that.cartList))
                  
                    $('#all_check').prop('checked',that.cartList.every(cart =>{
                        return cart.check
                    }))
                      localStorage.setItem('cart',JSON.stringify(that.cartList))
                    that.calcTotalMoney ()
                })
               
                // $('#all_check').on('change',function(){
                
                //         that.cartList.check = this.checked
                //         console.log(that.cartList.check)
                        
                //     localStorage.setItem('cart',JSON.stringify(that.cartList))
                // })  
                // console.log(that.cartList.check)



              }
        }




        new ShopCar()


    })
})